<?php
/**
 * Description of acl
 * Access control list
 *
 * @author dmitryy
 */
class Access_Acl extends Access_Db {
    function getArticle()
    {
        return $_SESSION['article'];
    }
    
    function getUserRoles($id) //gets all roles which user use
    {
        $select = array(
            'id',
            'role_id',
            'user_id'
        );
        $table = 'user_roles';
        $where = "user_id='$id'";
        $this->select($select, $table, $where);
        $result = array();
        while($row = $this->getSelected())
        {
            $result[] = $row;
        }
        return $result;
    }
    
    function getRolePermission($arr) //gets list of permissions for each role
    {
        $select = array(
            'id',
            'article_id',
            'role_id',
            'permission_id',
            'action',
            'controller'
        );
        $table = 'role_permissions';
        $result = array();
        foreach ($arr as $value)
        {
            $where = "role_id ='" . $value['role_id'] . "'";
            $this->select($select, $table, $where);
            while($row = $this->getSelected())
            {
                $result[] = $row;
            }
        }
        return $result;
    }
    
    function getPermissions($arr)
    {
        $result = array();
        foreach ($arr as $role)
        {
            $temp = $this->getRolePermission($role['role_id']);
            foreach ($temp as $perm)
            {
                $result[] = array(
                    $perm['controller'],                                        //0
                    $perm['action'],                                            //1
                    $perm['article_id'],                                        //2
                    $perm['permission_id']                                      //3
                );
            }
        }
        return $result;
    }

    function getAction()
    {
        return $_SESSION['action'];
    }
    
    function hasPermission($arr) //checking do user has access to content?
    {
        $url = parse_url($_SERVER['REQUEST_URI']);
        $path = explode("/", trim($url['path'], "/"));
        $path['0'] = ($path['0'] != NULL) ? $path['0'] : 'index' ; // (!!!)
        $path['1'] = (isset($path['1'])) ? $path['1'] : 'index' ;
        $path['2'] = (isset($path['2'])) ? $path['2'] : '0' ;
        // 0 - controller
        // 1 - action
        // 2 - id
        //var_dump($path);
        if((($path['0'] == 'index') and ($path['1'] == 'index')) or (($path['0'] == 'articles') and ($path['1'] == 'show')) or ($path['1'] == 'showsub'))
        {
            return TRUE; //allow
        }
        foreach ($arr as $value)
        {
            if(($path['0'] == $value['controller']) and ($path['1'] == $value['action']) and ($path['2'] == $value['article_id']))
            {
                return TRUE; //allow
            }
        }
        return FALSE; //denied
    }
}